﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AesonBaseFramework.EntityFramework.DataManagers;
using AesonBaseFramework.EntityFramework.DbContexts;
using AesonIdentityLibrary.DataContext;
using AesonIdentityLibrary.Repositories;

namespace AesonIdentityLibrary.DataManager
{
    public class IdentityDataManager : AesonBaseSqlDataManager<SecurityDbContext, IdentityDataManager>
    {
        public readonly SecurityDbContext _ctx;

        public bool DeleteDatabase()
        {
            if (_ctx.Database.Exists())
            {
                return _ctx.Database.Delete();
            }
            return true;


        }

        #region ctor
        public IdentityDataManager(SecurityDbContext t) : base(t)
        {
            _ctx = t;
        }

        public new static IdentityDataManager Create(DbConnectionInfo cn)
        {
            return  new IdentityDataManager(SecurityDbContext.Create(cn));
        }

        public new static IdentityDataManager Create(string user, string server, string database)
        {
            return new IdentityDataManager(SecurityDbContext.Create(DbConnectionInfo.Create(user,server,database)));
        }

        public new static IdentityDataManager Create(string user, string server, string database, string dbLogin, string dbPassword)
        {
            return new IdentityDataManager(SecurityDbContext.Create(DbConnectionInfo.Create(user, server, database,dbLogin,dbPassword)));
        }
        #endregion

        private UserClaimsRepository _claimsManager;
        public UserClaimsRepository ClaimsManager
        {
            get { return _claimsManager ?? (_claimsManager = new UserClaimsRepository(_ctx)); }
        }


        private ClientApplicationRepository _clientApplicatoinManager;
        public ClientApplicationRepository ClientApplicationManager
        {
            get  { return _clientApplicatoinManager ?? (_clientApplicatoinManager = new ClientApplicationRepository(_ctx)); }
        }


        private ApplicationUserManager _userManager;
        public ApplicationUserManager UserManager
        {
            get
            {
                if (_userManager == null)
                {
                    _userManager = ApplicationUserManager.Create(_ctx);
                }

                return _userManager;
            }
        }


        private ClaimsSecurityRepository _claimsSecurityManager;
        public ClaimsSecurityRepository ClaimsSecurityRepository
        {
            get { return _claimsSecurityManager ?? (_claimsSecurityManager = new ClaimsSecurityRepository(_ctx));}
        }

     
    }
}
